﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace TextEditor.TextDocument.Control
{
    public class AutoMsgBase : Label
    {
        protected ZYTextElement element = null;
        protected bool isSelected = false;

        public void DrawLine(Graphics g)
        {
            if (element != null && this.Visible == true)
            {
                Point point = element.OwnerDocument.OwnerControl.ViewPointToClient(0, element.OwnerLine.RealBottom);
                Point p = new Point(point.X + element.OwnerDocument.Pages.PaperWidth - element.OwnerDocument.Pages.LeftMargin + 5, point.Y - (this.Height / 2));
                this.Location = p;// new Point(point.X + base.OwnerDocument.Pages.PaperWidth - base.OwnerDocument.Pages.LeftMargin + 10, point.Y - pb.Height);
                foreach (System.Windows.Forms.Control control in element.OwnerDocument.OwnerControl.Controls)
                {
                    if (control is AutoMsgBase)
                    {
                        if (control == this)
                            break;
                        if (control.Location.Y >= this.Location.Y || this.Location.Y < control.Location.Y + control.Height)// && control.Location.Y + control.Height > pb.Location.Y)
                        {
                            this.Location = new Point(this.Location.X, control.Location.Y + control.Height);
                        }
                    }
                }
                Point p2 = new Point(element.OwnerDocument.Pages.StandardWidth, element.OwnerLine.RealTop + element.OwnerLine.Height - 1);
                using (Pen pen = new Pen(Color.Green))
                {
                    if (isSelected)
                    {
                        pen.Width = 2;
                        pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
                        pen.Color = Color.Red;
                    }
                    else
                    {
                        pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
                    }
                    Point p3 = element.OwnerDocument.OwnerControl.PointToView(this.Location.X, this.Location.Y);
                    g.DrawLine(pen, element.RealLeft, p2.Y, element.RealLeft, element.RealTop);
                    g.DrawLine(pen, element.RealLeft, p2.Y, p2.X, p2.Y);
                    g.DrawLine(pen, p2.X, p2.Y, element.OwnerDocument.Pages.StandardWidth + element.OwnerDocument.Pages.RightMargin + 5, p3.Y + (this.Height / 2));
                }
            }
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            isSelected = true;
            if (this.element != null)
            {
                element.OwnerDocument.OwnerControl.Refresh();
            }
            base.OnMouseEnter(e);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            isSelected = false;
            if (this.element != null)
            {
                element.OwnerDocument.OwnerControl.Refresh();
            }
            //base.OnLeave(e);
        }
    }
}
